package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.api.dao.mysql.IntegralLevelInfoDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.IntegralLevelInfo;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.dto.IntegralLevelAndTitleDTO;
import com.juyu.miliao.api.domain.vo.UserSortVo;
import com.juyu.miliao.api.service.IntegralLevelInfoService;
import com.juyu.miliao.api.util.CallTimeUtil;
import com.juyu.miliao.common.constant.CallTimeRankingListKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 通话时长Redis
 * @author xuanqicheng
 */
@Repository
public class CallTimeRedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private IntegralLevelInfoService integralLevelInfoService;



    public void setRankingList(LocalDate now, long seconds, String answerUserId) {

        setWeekRankingList(now,seconds,answerUserId);
        setDayRankingList(now,seconds,answerUserId);
    }

    private void setWeekRankingList(LocalDate now, long seconds, String answerUserId){
        String key = CallTimeRankingListKey.getWeekRankingListKey(now);
        stringRedisTemplate.opsForZSet().incrementScore(key,answerUserId,seconds);
        stringRedisTemplate.expire(key,30, TimeUnit.DAYS);
    }

    private void setDayRankingList(LocalDate now, long seconds, String answerUserId){
        String key = CallTimeRankingListKey.getDayRankingListKey(now);
        stringRedisTemplate.opsForZSet().incrementScore(key,answerUserId,seconds);
        stringRedisTemplate.expire(key,30, TimeUnit.DAYS);
    }
    //获取时长榜 两个周榜（本周or上周）
    public List<UserSortVo>  getWeekRankingList(LocalDate now, Integer startNum, Integer pageCount){
        String key = CallTimeRankingListKey.getWeekRankingListKey(now);
        return getUserSortVo(key,startNum,pageCount);
    }
    //获取时长榜 两个日榜（昨日or今日）
    public List<UserSortVo>  getDayRankingList(LocalDate now, Integer startNum, Integer pageCount){
        String key = CallTimeRankingListKey.getDayRankingListKey(now);
        return  getUserSortVo(key,startNum,pageCount);
    }

    /**
     * 查询时长榜 赋值并返回展示列表
     * @param key
     * @param startNum
     * @param pageCount
     * @return
     */
    public List<UserSortVo> getUserSortVo(String key,Integer startNum, Integer pageCount){
        List<UserSortVo> list = new ArrayList<>();
        Set<ZSetOperations.TypedTuple<String>> typedTuples = stringRedisTemplate.opsForZSet().reverseRangeWithScores(key, startNum, pageCount);
        Iterator<ZSetOperations.TypedTuple<String>> iterator = typedTuples.iterator();
        while (iterator.hasNext()) {
            UserSortVo userSortVo = new UserSortVo();
            ZSetOperations.TypedTuple<String> typedTuple = iterator.next();
            double score = typedTuple.getScore();
            System.out.println("秒："+score);
            String userId = typedTuple.getValue();
            User user = userInfoDao.getUserInfoById(Long.parseLong(userId));
            IntegralLevelAndTitleDTO userIntegralLevel = integralLevelInfoService.getUserIntegralLevel(user.getIntegralSum());
            userSortVo.setUserId(user.getUserId());
            userSortVo.setNiceName(user.getNickName());
            userSortVo.setIcon(user.getIcon());
            userSortVo.setLevel(userIntegralLevel.getIntegralGrade());
            userSortVo.setShowTime(CallTimeUtil.secondToFormTime((long) score));
            list.add(userSortVo);
        }
        return list;
    }
}
